package net.esj.basic.utils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

import net.esj.basic.annotation.IgnorePrint;
import net.esj.basic.core.view.tools.searcher.Searcher;
import net.esj.basic.core.view.tools.searcher.SearcherImpl;
import net.esj.basic.dao.hibernate.HibernateQueryBuilder;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.dao.querybuilder.QueryBuilderFactory;

@XmlRootElement
public class Pagination<T> {
	
	public static final Pagination EMPTY_PAGE = new Pagination();

	/**
	 * 默认每页行数
	 */
	public static final int DEFAULT_PAGE_SIZE = 10;
	
	/**
	 * 数据列
	 */
	private List<T> rows;
	
	/**
	 * 当前页
	 */
	private int page;
	
	/**
	 * 每页行数
	 */
	private int rp;
	
	/**
	 * 总数
	 */
	private int total;
	/**
	 * 总页数
	 */
	private int sum;
	
	@Deprecated
	private Map<String,Object> query;
	
	private Class<T> clazz;
	
	private QueryBuilder queryBuilder;
	
	private Searcher searcher = new SearcherImpl();
	
	public Pagination(){
		this.rp = DEFAULT_PAGE_SIZE;
		this.page = 1;
		rows = Collections.EMPTY_LIST;
	}
	
	public Pagination(int pageSize){
		this.rp = pageSize;
		this.page = 1;
		rows = Collections.EMPTY_LIST;
	}
	
	public Pagination(int pageSize,int pageNum){
		this.rp = pageSize;
		this.page = pageNum;
		rows = Collections.EMPTY_LIST;
	}
	
	public Pagination(Class<T> clazz){
		this(DEFAULT_PAGE_SIZE,clazz);
	}
	
	public Pagination(int pageSize,Class<T>clazz){
		this(pageSize, 1,clazz);
	}
	
	public Pagination(int pageSize,int pageNum,Class<T> clazz){
		this.rp = pageSize;
		this.page = pageNum;
		this.clazz = clazz;
		queryBuilder = QueryBuilderFactory.create(clazz);
		rows = Collections.EMPTY_LIST;
	}

	/**
	 * 每页行数
	 * @return
	 */
	public int getRp() {
		return rp;
	}

	public void setRp(int rp) {
		this.rp = rp;
	}

	/**
	 * 当前页
	 * @return
	 */
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * 总数
	 * @return
	 */
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	@IgnorePrint
	public Map<String, Object> getQuery() {
		return query;
	}

	public void setQuery(Map<String, Object> query) {
		this.query = query;
	}

	@IgnorePrint
	public Class getEntityClass(){
		return clazz;
	}
	
	public void setEntityClass(Class<T> clazz) {
		this.clazz = clazz;
	}

	/**
	 * 总页数
	 * @return
	 */
	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}

	@IgnorePrint
	public QueryBuilder getQueryBuilder() {
		return queryBuilder;
	}

	public void setQueryBuilder(QueryBuilder queryBuilder) {
		this.queryBuilder = queryBuilder;
	}
	
	/**
	 * 返回结果集是否不为空
	 * @return
	 */
	@IgnorePrint
	public boolean isNotEmpty(){
		return rows!=null && rows.size()>0;
	}
	
	/**
	 * 返回结果集是否为空
	 * @return
	 */
	@IgnorePrint
	public boolean isEmpty(){
		return !isNotEmpty();
	}

	@IgnorePrint
	public Searcher getSearcher() {
		return searcher;
	}

	public void setSearcher(Searcher searcher) {
		this.searcher = searcher;
	}
	
}
